package com.sinosoft.cloud.access.transformer.function;

import net.sf.saxon.s9api.*;

import static com.sinosoft.cloud.access.transformer.function.XSLTransfromFunction.*;

/**
 * Created by Jarod on 2016/12/3.
 */
public class SaxFunction {

    public static ExtensionFunction[] getFunctions() {

        ExtensionFunction yuanToFen = new ExtensionFunction() {
            @Override
            public QName getName() {
                return new QName("http://xml.apache.org/xslt/java", "com.sinosoft.cloud.access.transformer.function.XSLTransfromFunction.yuanToFen");
            }

            @Override
            public SequenceType getResultType() {
                return SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE);
            }

            @Override
            public SequenceType[] getArgumentTypes() {
                return new SequenceType[]
                        {
                                SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE)
                        };
            }

            @Override
            public XdmValue call(XdmValue[] arguments) throws SaxonApiException {
                String result = "";
                if (((XdmAtomicValue) arguments[0].itemAt(0)) != null) {
                    String arg = ((XdmAtomicValue) arguments[0].itemAt(0)).getStringValue();
                    result = String.valueOf(yuanToFen(arg));
                    return new XdmAtomicValue(result);
                } else {
                    return new XdmAtomicValue(result);
                }
            }
        };

        ExtensionFunction fenToYuan = new ExtensionFunction() {
            @Override
            public QName getName() {
                return new QName("http://xml.apache.org/xslt/java", "com.sinosoft.cloud.access.transformer.function.XSLTransfromFunction.fenToYuan");
            }

            @Override
            public SequenceType getResultType() {
                return SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE);
            }

            @Override
            public SequenceType[] getArgumentTypes() {
                return new SequenceType[]
                        {
                                SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ZERO_OR_ONE)
                        };
            }

            @Override
            public XdmValue call(XdmValue[] arguments) throws SaxonApiException {
                String result = "";
                if (arguments.length > 0
                        && arguments[0].size() > 0
                        && ((XdmAtomicValue) arguments[0].itemAt(0)) != null) {
                    String arg = ((XdmAtomicValue) arguments[0].itemAt(0)).getStringValue();
                    result = String.valueOf(fenToYuan(arg));
                    return new XdmAtomicValue(result);
                } else {
                    return new XdmAtomicValue(result);
                }
            }
        };

        ExtensionFunction fillStrWith_2Args = new ExtensionFunction() {
            @Override
            public QName getName() {
                return new QName("http://xml.apache.org/xslt/java", "com.sinosoft.cloud.access.transformer.function.XSLTransfromFunction.fillStrWith_2Args");
            }

            @Override
            public SequenceType getResultType() {
                return SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE);
            }

            @Override
            public SequenceType[] getArgumentTypes() {
                return new SequenceType[]
                        {
                                SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE)
                                , SequenceType.makeSequenceType(ItemType.INTEGER, OccurrenceIndicator.ONE)
                        };
            }

            @Override
            public XdmValue call(XdmValue[] arguments) throws SaxonApiException {
                String result = "";
                if (((XdmAtomicValue) arguments[0].itemAt(0)) != null && ((XdmAtomicValue) arguments[1].itemAt(0)) != null) {
                    String arg0 = ((XdmAtomicValue) arguments[0].itemAt(0)).getStringValue();
                    Integer arg1 = Integer.valueOf(((XdmAtomicValue) arguments[1].itemAt(0)).getStringValue());
                    result = fillStrWith_2Args(arg0, arg1);
                    return new XdmAtomicValue(result);
                } else {
                    return new XdmAtomicValue(result);
                }
            }
        };

        ExtensionFunction fillStrWith_3Args = new ExtensionFunction() {
            @Override
            public QName getName() {
                return new QName("http://xml.apache.org/xslt/java", "com.sinosoft.cloud.access.transformer.function.XSLTransfromFunction.fillStrWith_");
            }

            @Override
            public SequenceType getResultType() {
                return SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE);
            }

            @Override
            public SequenceType[] getArgumentTypes() {
                return new SequenceType[]
                        {
                                SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE)
                                , SequenceType.makeSequenceType(ItemType.INTEGER, OccurrenceIndicator.ONE)
                                , SequenceType.makeSequenceType(ItemType.BOOLEAN, OccurrenceIndicator.ONE)
                        };
            }

            @Override
            public XdmValue call(XdmValue[] arguments) throws SaxonApiException {
                String result = "";
                if (((XdmAtomicValue) arguments[0].itemAt(0)) != null && ((XdmAtomicValue) arguments[1].itemAt(0)) != null) {
                    String arg0 = ((XdmAtomicValue) arguments[0].itemAt(0)).getStringValue();
                    Integer arg1 = Integer.valueOf(((XdmAtomicValue) arguments[1].itemAt(0)).getStringValue());
                    Boolean arg2 = Boolean.valueOf(((XdmAtomicValue) arguments[2].itemAt(0)).getStringValue());
                    if (arg2 == null) {
                        arg2 = true;
                    }
                    result = fillStrWith_(arg0, arg1, arg2);
                    return new XdmAtomicValue(result);
                } else {
                    return new XdmAtomicValue(result);
                }
            }
        };

        ExtensionFunction date8to10 = new ExtensionFunction() {
            @Override
            public QName getName() {
                return new QName("http://xml.apache.org/xslt/java", "com.sinosoft.cloud.access.transformer.function.XSLTransfromFunction.date8to10");
            }

            @Override
            public SequenceType getResultType() {
                return SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE);
            }

            @Override
            public SequenceType[] getArgumentTypes() {
                return new SequenceType[]
                        {
                                SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE)
                        };
            }

            @Override
            public XdmValue call(XdmValue[] arguments) throws SaxonApiException {
                String result = "";
                if (((XdmAtomicValue) arguments[0].itemAt(0)) != null) {
                    String arg0 = ((XdmAtomicValue) arguments[0].itemAt(0)).getStringValue();
                    result = date8to10(arg0);
                    return new XdmAtomicValue(result);
                } else {
                    return new XdmAtomicValue(result);
                }
            }
        };

        ExtensionFunction date10to8 = new ExtensionFunction() {
            @Override
            public QName getName() {
                return new QName("http://xml.apache.org/xslt/java", "com.sinosoft.cloud.access.transformer.function.XSLTransfromFunction.date10to8");
            }

            @Override
            public SequenceType getResultType() {
                return SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE);
            }

            @Override
            public SequenceType[] getArgumentTypes() {
                return new SequenceType[]
                        {
                                SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE)
                        };
            }

            @Override
            public XdmValue call(XdmValue[] arguments) throws SaxonApiException {
                String result = "";
                if (((XdmAtomicValue) arguments[0].itemAt(0)) != null) {
                    String arg0 = ((XdmAtomicValue) arguments[0].itemAt(0)).getStringValue();
                    result = date10to8(arg0);
                    return new XdmAtomicValue(result);
                } else {
                    return new XdmAtomicValue(result);
                }
            }
        };

        ExtensionFunction parsBoolean = new ExtensionFunction() {
            @Override
            public QName getName() {
                return new QName("http://xml.apache.org/xslt/java", "com.sinosoft.cloud.access.transformer.function.XSLTransfromFunction.parseBoolean");
            }

            @Override
            public SequenceType getResultType() {
                return SequenceType.makeSequenceType(ItemType.BOOLEAN, OccurrenceIndicator.ONE);
            }

            @Override
            public SequenceType[] getArgumentTypes() {
                return new SequenceType[]
                        {
                                SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE)
                        };
            }

            @Override
            public XdmValue call(XdmValue[] arguments) throws SaxonApiException {
                Boolean result = null;
                if (((XdmAtomicValue) arguments[0].itemAt(0)) != null) {
                    String arg = ((XdmAtomicValue) arguments[0].itemAt(0)).getStringValue();
                    result = parseBoolean(arg);
                    return new XdmAtomicValue(result);
                } else {
                    return new XdmAtomicValue(result);
                }
            }
        };

        ExtensionFunction appntIncome = new ExtensionFunction() {
            @Override
            public QName getName() {
                return new QName("http://xml.apache.org/xslt/java", "com.sinosoft.cloud.access.transformer.function.XSLTransfromFunction.appntIncomeFormat");
            }

            @Override
            public SequenceType getResultType() {
                return SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE);
            }

            @Override
            public SequenceType[] getArgumentTypes() {
                return new SequenceType[]
                        {
                                SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE)
                                ,SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE)
                        };
            }

            @Override
            public XdmValue call(XdmValue[] arguments) throws SaxonApiException {
                String result = "";
                if (((XdmAtomicValue) arguments[0].itemAt(0)) != null && ((XdmAtomicValue) arguments[1].itemAt(0)) != null) {
                    String arg0 = ((XdmAtomicValue) arguments[0].itemAt(0)).getStringValue();
                    String arg1 = ((XdmAtomicValue) arguments[1].itemAt(0)).getStringValue();
                    if (null == arg1 || "".equals(arg1))
                    {
                        arg1 = "";
                    }
                    try {
                        result = appntIncomeFormat(arg0, arg1);
                    }catch (NumberFormatException e){
                        result = arg1;
                        e.printStackTrace();
                    }
                    return new XdmAtomicValue(result);
                } else {
                    return new XdmAtomicValue(result);
                }
            }
        };

        ExtensionFunction insuredIncome = new ExtensionFunction() {
            @Override
            public QName getName() {
                return new QName("http://xml.apache.org/xslt/java", "com.sinosoft.cloud.access.transformer.function.XSLTransfromFunction.insuredIncomeFormat");
            }

            @Override
            public SequenceType getResultType() {
                return SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE);
            }

            @Override
            public SequenceType[] getArgumentTypes() {
                return new SequenceType[]
                        {
                                SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE)
                                ,SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE)
                        };
            }

            @Override
            public XdmValue call(XdmValue[] arguments) throws SaxonApiException {
                String result = "";
                if (((XdmAtomicValue) arguments[0].itemAt(0)) != null && ((XdmAtomicValue) arguments[1].itemAt(0)) != null) {
                    String arg0 = ((XdmAtomicValue) arguments[0].itemAt(0)).getStringValue();
                    String arg1 = ((XdmAtomicValue) arguments[1].itemAt(0)).getStringValue();
                    if (null == arg1 || "".equals(arg1))
                    {
                        arg1 = "";
                    }
                    result = insuredIncomeFormat(arg0, arg1);
                    return new XdmAtomicValue(result);
                } else {
                    return new XdmAtomicValue(result);
                }
            }
        };

        ExtensionFunction appntFamilyIncome = new ExtensionFunction() {
            @Override
            public QName getName() {
                return new QName("http://xml.apache.org/xslt/java", "com.sinosoft.cloud.access.transformer.function.XSLTransfromFunction.appntFamilyIncomeFormat");
            }

            @Override
            public SequenceType getResultType() {
                return SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE);
            }

            @Override
            public SequenceType[] getArgumentTypes() {
                return new SequenceType[]
                        {
                                SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE)
                        };
            }

            @Override
            public XdmValue call(XdmValue[] arguments) throws SaxonApiException {
                String result = "";
                if (((XdmAtomicValue) arguments[0].itemAt(0)) != null) {
                    String arg0 = ((XdmAtomicValue) arguments[0].itemAt(0)).getStringValue();
                    result = appntFamilyIncomeFormat(arg0);
                    return new XdmAtomicValue(result);
                } else {
                    return new XdmAtomicValue(result);
                }
            }
        };

        ExtensionFunction fixedLengthString = new ExtensionFunction() {
            @Override
            public QName getName() {
                return new QName("http://xml.apache.org/xslt/java", "com.sinosoft.cloud.access.transformer.function.XSLTransfromFunction.fixedLengthString");
            }

            @Override
            public SequenceType getResultType() {
                return SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE);
            }

            @Override
            public SequenceType[] getArgumentTypes() {
                return new SequenceType[]
                        {
                                SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE),
                                SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE)
                        };
            }

            @Override
            public XdmValue call(XdmValue[] arguments) throws SaxonApiException {
                String result = "";
                if (((XdmAtomicValue) arguments[0].itemAt(0)) != null) {
                    String arg = ((XdmAtomicValue) arguments[0].itemAt(0)).getStringValue();
                    String arg2 = ((XdmAtomicValue) arguments[1].itemAt(0)).getStringValue();
                    result = String.valueOf(fixedLengthString(arg,arg2));
                    return new XdmAtomicValue(result);
                } else {
                    return new XdmAtomicValue(result);
                }
            }
        };

        return new ExtensionFunction[]{
                yuanToFen, fenToYuan, fillStrWith_2Args, fillStrWith_3Args, date8to10, parsBoolean
                , date10to8, appntIncome, insuredIncome,appntFamilyIncome,fixedLengthString
        };
    }
}